import { defineSeed } from "@/seed-application/types";
import SetActions from "../components/SetActions.vue";
import SetDatas from "../components/SetDatas.vue";
import SetFields from '../components/SetFields.vue'
import { open } from "@/components/Popup";
import feedback from "@/utils/feedback";

export default defineSeed({
  name: 'user',
  label: '租户',
  icon: 'majesticons:bus',
  comment: '',
  declares: ['organization','name','logo','tenantname','expire_date','usable','loginable','enable','active'],
  checkLogin: false,
  checkAction: false,

  actions: {
    page: {
      label: '租户列表',
      type: 'page',
      icon: 'ep:plus'
    },
    save: {
      label: '新增',
      type: 'save',
      declares: ['organization_id', 'name', 'tenantname', 'db_url', 'db_username', 'db_password', 'remark'],
      componentProps: {maxHeight: '400px', width: '40vw'}
    },
    update: {
      label: '更新',
      type: 'update',
      declares: ['name', 'remark'],
      componentProps: {maxHeight: '200px', width: '40vw'}
    },
    logo: {
      label: '设置LOGO',
      type: 'set',
      componentProps: {maxHeight: '80px', width: '30vw'}
    },
    tenantname: {
      label: '设置账号',
      type: 'set',
      componentProps: {maxHeight: '80px', width: '40vw'}
    },
    reset: {
      label: '重置密码',
      type: 'reset'
    },
    expireDate: {
      label: '设置截止日期',
      type: 'set',
      declare: 'expire_date',
      componentProps: {maxHeight: '60vh', width: '40vw'}
    },
    usable: {
      label: '设置启用',
      type: 'update',
      declares: ['usable', 'loginable', 'enable', 'reason'],
      componentProps: {maxHeight: '300px', width: '40vw'}
    },
    active: {
      label: '激活账号',
      type: 'set',
      declareValue: 'yes'
    },
    roles: {
      label: '分配角色',
      type: 'set',
      declare: 'roles'
    },
    actions: {
      label: '分配功能权限',
      onClick: (action, unit, row) => {
        if(row) {
          open(SetActions, {title: action.label, action, unit, detail: row, width: '25vw'})
        } else {
          feedback.alertSelect()
        }
      }
    },
    datas: {
      label: '分配数据权限',
      onClick: (action, unit, row) => {
        if(row) {
          open(SetDatas, {title: action.label, action, unit, detail: row})
        } else {
          feedback.alertSelect()
        }
      }
    },
    fields: {
      label: '分配字段权限',
      onClick: (action, unit, row) => {
        if(row) {
          open(SetFields, {title: action.label, action, unit, detail: row})
        } else {
          feedback.alertSelect()
        }
      }
    },
    detail: {
      label: '详情',
      type: 'detail'
    },
    remove: {
      label: '删除',
      type: 'remove'
    }
  },

  columns: {
    id: {type: 'char', length: 17, required: true, label: 'ID'},
    organization_id: {type: 'ref', rel: 'organization.tree.id', length: 17, required: true, label: '所属部门'},
    name: {type: 'varchar', length: 64, label: '名称'},
    remark: {type: 'text', label: '备注'},
    logo: {type: 'avatar', length: 128, label: 'LOGO'},
    db_url: {type: 'varchar', length: 128, label: '数据库地址'},
    db_username: {type: 'varchar', length: 64, label: '数据库用户名'},
    db_password: {type: 'password', length: 64, label: '数据库密码'},
    tenantname: {type: 'varchar', length: 64, label: '租户名', required: true},
    password: {type: 'varchar', length: 64, label: '密码'},
    salt: {type: 'varchar', length: 64, label: '盐'},
    expire_date: {type: 'date', label: '截止日期'},
    usable: {type: 'yesno', length: 8, label: '是否可用'},
    loginable: {type: 'yesno', length: 8, label: '是否能登录'},
    enable: {type: 'yesno', length: 8, label: '是否启用'},
    active: {type: 'yesno', length: 8, label: '是否激活'},
    reason: {type: 'text', label: '原因'},
    tenant_id: {type: 'char', length: 17, label: '所属租户'},
    created_at: {type: 'datetime', label: '创建时间'},
    updated_at: {type: 'datetime', label: '更新时间'},
    deleted_at: {type: 'datetime', label: '删除时间'},
    version: {type: 'int', label: '版本号'}
  },

  belongsTo: {
    organization: {seedName: 'organization', selfColumn: "organization_id", targetColumn: "id", comment: "所属组织"}
  },
  habtm: {
    roles: {seedName: 'role', throughSeedName: 'userRole'}
  }
})